package com.mba.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.mba.activity.R;
import com.mba.activity.UserMainPageActivity;
import com.mba.base.MyOnClickLisenter;
import com.mba.bean.Message;
import com.mba.util.AsyncImageLoader;
import com.mba.util.AsyncImageLoader.ImageCallback;
import com.mba.widget.JListView;

public class HomeListAdapter extends BaseAdapter {
	
	//图片的异步下载类
    AsyncImageLoader asyncImageLoader;
    
	private Context context;
	private ArrayList<Message> contentList;
	public HomeListAdapter(Context context, ArrayList<Message> contentList) {
		this.context=context;
		this.contentList = contentList;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return contentList!=null?contentList.size():0;
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@SuppressLint("SimpleDateFormat")
	@Override
	public View getView(int arg0, View arg1, final ViewGroup arg2) {
		if(arg1==null){
			arg1=LayoutInflater.from(context).inflate(R.layout.home_list_item_layout, null);
		}
		
		final ImageView avater = (ImageView) arg1.findViewById(R.id.home_list_item_avatar);
		TextView name = (TextView) arg1.findViewById(R.id.home_list_item_name);
		TextView area = (TextView) arg1.findViewById(R.id.home_list_item_area);
		TextView content = (TextView) arg1.findViewById(R.id.home_list_item_content);
		TextView contentCreateTime = (TextView) arg1.findViewById(R.id.home_list_itme_content_create_time);
		JListView reposts = (JListView) arg1.findViewById(R.id.home_list_report_list);
		final Message info = contentList.get(arg0);
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm");
		contentCreateTime.setText(sdf.format(info.getCreateAt()));
		//点击这里显示地图位置
		area.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
			
			}
		});
		content.setTag(info.getMessageID());
		content.setOnClickListener(new MyOnClickLisenter(content,arg0));
		
		asyncImageLoader = new AsyncImageLoader();
		//异步加载图片
		 Drawable avater_image = asyncImageLoader.loadDrawable(
                 info.getUser().getHeadPic(), avater,
                 new ImageCallback() {
                     public void imageLoaded(Drawable drawable,
                             ImageView iv, String url) {
                         iv.setImageDrawable(drawable);
                     }
        });
		if (avater_image != null) {
			avater.setImageDrawable(avater_image);	
		}
		
		avater.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(arg0.getContext(),UserMainPageActivity.class);
				Activity activity = (Activity) arg0.getContext();
				activity.startActivity(intent);
			}
		});
		
		name.setText(info.getUser().getUserName());
    	area.setText(info.getLocation());
		content.setText(info.getText());
		if(info.getComments() != null && info.getComments().size()>0){
			CommentListAdapter commentAdp = new CommentListAdapter(context,info);
			//回复布局
			reposts.setAdapter(commentAdp);
		}
		else{
			reposts.setVisibility(View.INVISIBLE);
		}
		
		return arg1;
	}

}
